#include "gameboard.h"
#include "cell.h"
#include <cstdlib>

#include <QVBoxLayout>
#include <QGridLayout>
#include <QPushButton>
#include <QLabel>
#include <QKeyEvent>
#include <QVBoxLayout>
#include <QString>

#include <QDebug>

bool leftInvalid = 0;
bool rightInvalid = 0;
bool upInvalid = 0;
bool downInvalid = 0;

GameBoard::GameBoard(QWidget *parent) : // 由Qwidget继承来的组件
    QWidget(parent)
{
    // 设置主窗口尺寸
    resize(650,650); // 初始窗口大小

    // 设置主窗口垂直布局
    mainLayout = new QVBoxLayout(); // QLayout将Widget呈现垂直排列
    setLayout(mainLayout);

    // 创建游戏面板为网格状布局
    boardLayout = new QGridLayout(); // QLayout将Widget呈现二维网格排列

    drawBoard(); //在游戏面板上生成随机的数字

    mainLayout->insertLayout(0, boardLayout); // 在主窗口中插入游戏面板布局

    // 加入score显示模块
    score = new QLabel(QString("SCORE: %1").arg(0)); // 用QLabel显示分数score
    score->setStyleSheet("QLabel { color: rgb(235,224,214); font: 16pt; }");
    score->setFixedHeight(50);
    mainLayout->insertWidget(1, score, 0, Qt::AlignRight); // 在主窗口中插入分数模块

    // 设置游戏面板背景
    setStyleSheet("GameBoard { background-color: rgb(187,173,160) }");

    // 连接游戏失败窗口
    connect(gameOverWindow.getResetBtn(), SIGNAL(clicked()), this, SLOT(resetGameOver()));

    // 连接游戏成功窗口
    connect(gameWinWindow.getResetBtn(), SIGNAL(clicked()), this, SLOT(resetGameWin()));
}

GameBoard::~GameBoard(){
    for (int i = 0; i < NCells; ++i) {
        for (int j = 0; j < NCells; ++j) {
            delete cells[i][j];
        }
    }
}

void GameBoard::drawBoard()
{
    delete boardLayout;
    boardLayout = new QGridLayout();
    for (int i = 0; i < NCells; ++i) {
        for (int j = 0; j < NCells; ++j) {
            delete cells[i][j];
            cells[i][j] = new Cell(game.board[i][j]);
            boardLayout->addWidget(cells[i][j], i, j);
            cells[i][j]->draw();
        }
    }
    mainLayout->insertLayout(0, boardLayout);
}

void GameBoard::keyPressEvent(QKeyEvent *event)
{
    switch (event->key()) {
    case Qt::Key_Up:
        game.move(false, false);
        break;
    case Qt::Key_Down:
        game.move(false, true);
        break;
    case Qt::Key_Left:
        game.move(true, false);
        break;
    case Qt::Key_Right:
        game.move(true, true);
        break;
    }
    if (game.changed) {
        if(game.wonGame == 1){
            game.wonGame = 0;
            gameWinWindow.show();
        }
        upInvalid = 0;
        downInvalid = 0;
        leftInvalid = 0;
        rightInvalid = 0;
        if (!game.isfull()) {
            auto pos = game.getEmptyPos();
            game.board[pos[0]][pos[1]] = 2;
        }
        score->setText(QString("SCORE: %1").arg(game.total_score));
        drawBoard();
    }
    else if(game.isfull()){
        if(event->key() == Qt::Key_Up) upInvalid = 1;
        if(event->key() == Qt::Key_Down) downInvalid = 1;
        if(event->key() == Qt::Key_Left) leftInvalid = 1;
        if(event->key() == Qt::Key_Right) rightInvalid = 1;
        if(upInvalid + downInvalid + leftInvalid + rightInvalid == 4) gameOverWindow.show();
    }
}

void GameBoard::resetGameOver()
{
    game.resetGame();
    drawBoard();
    score->setText(QString("SCORE: %1").arg(game.total_score));
    gameOverWindow.hide();
}

void GameBoard::resetGameWin()
{
    game.resetGame();
    drawBoard();
    score->setText(QString("SCORE: %1").arg(game.total_score));
    gameWinWindow.hide();
}
